package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.model.goods.Goods;
import cn.edu.scnu.semart.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品榜单统计Controller
 */
@RestController
@RequestMapping("/ranking")
//@CrossOrigin
public class GoodsRankingController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取销量排行榜
     */
    @GetMapping("/sales")
    public Map<String, Object> getSalesRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> salesRanking = goodsService.getSalesRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", salesRanking);
            result.put("message", "获取销量排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取点赞排行榜
     */
    @GetMapping("/likes")
    public Map<String, Object> getLikesRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> likesRanking = goodsService.getLikesRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", likesRanking);
            result.put("message", "获取点赞排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取收藏排行榜
     */
    @GetMapping("/favorites")
    public Map<String, Object> getFavoritesRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> favoritesRanking = goodsService.getFavoritesRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", favoritesRanking);
            result.put("message", "获取收藏排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取热门商品排行榜
     */
    @GetMapping("/hot")
    public Map<String, Object> getHotRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> hotRanking = goodsService.getHotRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", hotRanking);
            result.put("message", "获取热门商品排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取新品排行榜
     */
    @GetMapping("/new")
    public Map<String, Object> getNewRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> newRanking = goodsService.getNewRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", newRanking);
            result.put("message", "获取新品排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取价格排行榜（从低到高）
     */
    @GetMapping("/price/low")
    public Map<String, Object> getLowPriceRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> lowPriceRanking = goodsService.getLowPriceRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", lowPriceRanking);
            result.put("message", "获取低价排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取价格排行榜（从高到低）
     */
    @GetMapping("/price/high")
    public Map<String, Object> getHighPriceRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> highPriceRanking = goodsService.getHighPriceRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", highPriceRanking);
            result.put("message", "获取高价排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取综合排行榜
     */
    @GetMapping("/comprehensive")
    public Map<String, Object> getComprehensiveRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> comprehensiveRanking = goodsService.getComprehensiveRanking(limit, categoryId);
            result.put("success", true);
            result.put("data", comprehensiveRanking);
            result.put("message", "获取综合排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取分类排行榜
     */
    @GetMapping("/category/{categoryId}")
    public Map<String, Object> getCategoryRanking(
            @PathVariable Integer categoryId,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "sales") String sortBy) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<Goods> categoryRanking = goodsService.getCategoryRanking(categoryId, limit, sortBy);
            result.put("success", true);
            result.put("data", categoryRanking);
            result.put("message", "获取分类排行榜成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取榜单统计信息
     */
    @GetMapping("/statistics")
    public Map<String, Object> getRankingStatistics() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> statistics = goodsService.getRankingStatistics();
            result.put("success", true);
            result.put("data", statistics);
            result.put("message", "获取榜单统计信息成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 